package com.netease.nim.demo.session.announcement;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.netease.nim.demo.team.model.Announcement;
import com.shwread.android.qysw10000243.R;

import java.util.List;

public class AnnouncementView {

    public interface AnnouncementOnClickListener {
        public void onAnnouncementClick(String id);
    }

    private ViewPager vp;
    private LinearLayout indicator;
    private int pageCount;
    private int maxPageCount;

    private Context context;
    private List<Announcement> data;
    private AnnouncementPaperAdapter pagerAdapter = new AnnouncementPaperAdapter();
    private AnnouncementOnClickListener listener;

    public AnnouncementView(Context context, ViewPager mCurPage,
                            LinearLayout mLayoutBottom, List<Announcement> data,
                            AnnouncementOnClickListener listener, int maxPageCount) {
        this.context = context.getApplicationContext();
        this.indicator = mLayoutBottom;
        this.vp = mCurPage;
        this.data = data;
        this.listener = listener;
        this.pageCount = data.size();
        this.maxPageCount = maxPageCount;

        vp.setOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                setCurrentPage(position);
            }

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
        vp.setAdapter(pagerAdapter);
        vp.setOffscreenPageLimit(maxPageCount);
    }

    public void showAnnouncement() {
        pagerAdapter.notifyDataSetChanged();
        resetViewPager();
    }

    private void resetViewPager() {
        setCurrentPage(0);
        vp.setCurrentItem(0, false);
    }

    private void setCurrentPage(int page) {
        int hasCount = indicator.getChildCount();
        int forMax = Math.max(hasCount, pageCount);

        ImageView imgCur;
        for (int i = 0; i < forMax; i++) {
            if (pageCount <= hasCount) {
                if (i >= pageCount) {
                    indicator.getChildAt(i).setVisibility(View.GONE);
                    continue;
                } else {
                    imgCur = (ImageView) indicator.getChildAt(i);
                }
            } else {
                if (i < hasCount) {
                    imgCur = (ImageView) indicator.getChildAt(i);
                } else {
                    imgCur = new ImageView(context);
                    imgCur.setBackgroundResource(R.drawable.emoji_image_view_selector);
                    indicator.addView(imgCur);
                }
            }

            imgCur.setId(i);
            imgCur.setSelected(i == page); // 判断当前页码来更新
            imgCur.setVisibility(View.VISIBLE);
        }
    }

    /**
     * ***************************** PagerAdapter ****************************
     */
    private class AnnouncementPaperAdapter extends PagerAdapter {
        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public int getCount() {
            return pageCount >= maxPageCount ? maxPageCount : pageCount;
        }

        @Override
        public Object instantiateItem(ViewGroup container, final int position) {
            final Announcement a = data.get(position);

            View root = View.inflate(context, R.layout.team_announcement_vp_item, null);
            TextView titleText = (TextView) root.findViewById(R.id.announce_title);
            titleText.setText(a.getTitle());
            TextView contentText = (TextView) root.findViewById(R.id.announce_content);
            contentText.setText(a.getContent());
            root.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onAnnouncementClick(a.getId());
                }
            });

            container.addView(root);
            return root;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            View layout = (View) object;
            container.removeView(layout);
        }

        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }
    }
}
